package com.rw.coco.handler.message.event;

import com.rw.coco.api.data.CocoDataApi;
import com.rw.coco.api.data.model.GroupModel;
import com.rw.coco.api.event.GroupMessageEvent;
import com.rw.coco.api.event.MessageEvent;
import com.rw.coco.api.log.CocoLogger;
import com.rw.coco.api.modle.Group;
import com.rw.coco.api.reply.CocoReplyApi;
import com.rw.coco.util.CocoData;

/**
 * 可可群消息对象
 */
public class CocoGroupMessageEvent extends CocoMessageEvent implements GroupMessageEvent {
    private final GroupModel groupModel;

    private final GroupMessageEvent groupMessage;

    public CocoGroupMessageEvent(GroupMessageEvent groupMessage, CocoDataApi dataApi,
                                 CocoReplyApi replyApi, CocoLogger logger, CocoData cocoData) {
        super(groupMessage, dataApi, replyApi, logger, cocoData);
        this.groupMessage = groupMessage;

        // 获取群组信息
        String groupId = groupMessage.getGroup().getId();
        groupModel = dataApi.getPersistenceMapper().getGroupMapper().getGroupOrPut(groupId);
    }

    @Override
    public boolean isBlack() {
        // 用户不是超级管理员，并且群或用户在黑名单内
        return !accountModel.getSuperAdmin() && (accountModel.getBlackList() || groupModel.getBlackList());
    }

    @Override
    public MessageEvent getMessageEvent() {
        return getGroupMessage();
    }

    public GroupModel getGroupModel() {
        return groupModel;
    }

    public GroupMessageEvent getGroupMessage() {
        return groupMessage;
    }

    /**
     * 获取本次的群聊群信息
     */
    public Group getGroup() {
        return getGroupMessage().getGroup();
    }

    /**
     * 本次的消息是否由管理员或群主发送
     */
    public boolean isAdmin() {
        return getGroupMessage().isAdmin();
    }

    /**
     * 退出该群
     */
    public void quit() {
        getGroupMessage().quit();
    }

    /**
     * 向群组发消息
     *
     * @param message 需要发送的消息
     * @param groupId 发送的群组ID
     */
    @Override
    public void sendGroup(String message, String groupId) {
        groupMessage.sendGroup(message, groupId);
    }
}
